﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Boggle
{
	public sealed class Die : INotifyPropertyChanged
	{
		private readonly static Random random;

		private readonly String[] faces;

		public String CurrentValue
		{
			get;
			private set;
		}

		static Die()
		{
			random = new Random();
		}

		public Die(params String[] _faces)
		{
			if (_faces == null)
			{
				throw new ArgumentNullException("_faces");
			}
			if (_faces.Length < 1)
			{
				throw new ArgumentException("_faces contains no values");
			}
            if (_faces.Any(String.IsNullOrEmpty))
            {
                throw new ArgumentException("_faces contains null or empty string");
            }
			
			faces = _faces;
            CurrentValue = faces[0];
		}

		public void Roll()
		{
			var oldValue = CurrentValue;
			var newValue = faces[random.Next(faces.Length)];
			CurrentValue = newValue;

			if (oldValue != newValue)
			{
				OnPropertyChanged(new PropertyChangedEventArgs("CurrentValue"));
			}
		}

        public override string ToString()
        {
            return String.Format("({0}) - [{1}]", CurrentValue, String.Join(",", faces));
        }

		#region INotifyPropertyChanged Implementation

		public event PropertyChangedEventHandler PropertyChanged;

		private void OnPropertyChanged(PropertyChangedEventArgs e)
		{
			var handler = PropertyChanged;

			if (handler != null)
			{
				handler(this, e);
			}
		}

		#endregion
	}
}
